﻿using MediatR;
using ProcurementManagement.Applcation.Command.PurchaseContractsCommand;
using ProcurementManagement.Infrstructre.Interface;

namespace ProcurementManagement.Applcation.CommandHanlers.PurchaseContractsCommandHandler
{
    /// <summary>
    /// 删除采购合同命令处理器
    /// </summary>
    public class PurchaseContractsDelCommandHandler:IRequestHandler<PurchaseContractsDelCommand,int>
    {
        private readonly IPurchaseContractsRepository _purchaseContractsRep;

        public PurchaseContractsDelCommandHandler(IPurchaseContractsRepository purchaseContractsRep)
        {
            this._purchaseContractsRep = purchaseContractsRep;
        }

        public async Task<int> Handle(PurchaseContractsDelCommand request, CancellationToken cancellationToken)
        {
            var res = (await _purchaseContractsRep.GetAll()).Where(x => request.PurchaseContractsIds.Contains(x.PurchaseContractsIds)).ToList();
            foreach (var item in res)
            {
                item.IsDel = true;
                item.DelName = request.DelName;
                item.DelTime = DateTime.Now;
                await _purchaseContractsRep.UpdataAsync(item);
            }
            return 1;
        }
    }
}
